/*
 * @file com.chuanlian.mini.pay.notify.RefundNotifyHandleImpl.java
 * @date 2022/3/8
 * @author lhz
 * @copyright 马丁洛克 Corporation 2022 版权所有
 * @version 0.0.1
 */
package com.sinux.generality.provider.pay.notify;

import com.alibaba.fastjson.JSONObject;
import com.sinux.generality.provider.pay.constants.NumberNum;
import com.sinux.generality.provider.pay.entity.GptPayOrder;
import com.sinux.generality.provider.pay.entity.GptPayOrderRefund;
import com.sinux.generality.provider.pay.entity.GptPayPrepay;
import com.sinux.generality.provider.pay.factory.enums.NotifyStateEnums;
import com.sinux.generality.provider.pay.mapper.GptPayOrderMapper;
import com.sinux.generality.provider.pay.mapper.GptPayOrderRefundMapper;
import com.sinux.generality.provider.pay.mapper.GptPayPrepayMapper;
import com.sinux.generality.provider.pay.service.PayOrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author lhz
 * @version 0.0.1
 * @className RefundNotifyHandleImpl
 * @describe 退款回调通知
 * @date 2022/3/8 10:49
 */
@Service
public class RefundNotifyHandleImpl implements RefundNotifyHandle {
    @Resource
    private GptPayOrderMapper gptPayOrderMapper;

    @Resource
    private GptPayPrepayMapper gptPayPrepayMapper;
    @Resource
    private GptPayOrderRefundMapper hospitalPayOrderRefundMapper;
    @Resource
    private PayOrderService payOrderService;
    private final static Logger logger = LoggerFactory.getLogger(RefundNotifyHandleImpl.class);

    /****
     * notifyHandle 回调处理
     * @author lhz
     * @param json
     * @return java.util.Map<java.lang.String, java.lang.Object>
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void notifyHandle(String json) {
        logger.info(json);
        //处理业务
        JSONObject jsonObject = JSONObject.parseObject(json);
        //商户预支付订单号
        String outTradeNo = jsonObject.getString("out_trade_no");
        //退款单号
        String outRefundNo = jsonObject.getString("out_refund_no");
        String successTime = jsonObject.getString("success_time");
        //退款状态
        String refundStatus = jsonObject.getString("refund_status");
        //处理退款成功
        if (refundStatus.equals(NotifyStateEnums.SUCCESS.getCode())) {
            LocalDateTime parse = LocalDateTime.parse(successTime, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
            //查询预支付单
            GptPayPrepay gptPayPrepay = gptPayPrepayMapper.selectById(outTradeNo);
            GptPayOrder hospitalPayOrder = this.gptPayOrderMapper.selectById(gptPayPrepay.getPayOrderId());
            GptPayOrderRefund orderRefund = this.hospitalPayOrderRefundMapper.selectById(outRefundNo);
            if (orderRefund.getRefundStatus()!= NumberNum.INT_1){
                return;
            }
            orderRefund.setSuccessTime(parse);
            orderRefund.setRefundStatus(2);
            hospitalPayOrderRefundMapper.updateById(orderRefund);
            BigDecimal refundMoneyTotal = hospitalPayOrder.getRefundMoney();
            BigDecimal totalPrice = hospitalPayOrder.getTotal();
            BigDecimal add = refundMoneyTotal.add(orderRefund.getPayerRefund());
            //比较支付订单总金额与退款总金额大小
            if (totalPrice.compareTo(add)==0){
                hospitalPayOrder.setRefundState(2);
            }else if (totalPrice.compareTo(add)==1){
                hospitalPayOrder.setRefundState(1);
            }
            hospitalPayOrder.setRefundMoney(add);
            this.gptPayOrderMapper.updateById(hospitalPayOrder);

        }
    }
}